<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>游戏背景音乐实现方案</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
            padding: 20px;
        }
        
        .container {
            max-width: 800px;
            width: 100%;
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            overflow: hidden;
        }
        
        header {
            background: linear-gradient(to right, #3498db, #2c3e50);
            color: white;
            padding: 25px 30px;
            text-align: center;
        }
        
        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
        }
        
        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
        }
        
        .content {
            padding: 30px;
        }
        
        .section {
            margin-bottom: 30px;
            padding: 20px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
        }
        
        h2 {
            color: #2c3e50;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #3498db;
        }
        
        .code-block {
            background-color: #2c3e50;
            color: #ecf0f1;
            padding: 15px;
            border-radius: 8px;
            margin: 15px 0;
            overflow-x: auto;
            font-family: 'Courier New', monospace;
            font-size: 0.95rem;
            line-height: 1.5;
        }
        
        .controls {
            display: flex;
            justify-content: center;
            gap: 15px;
            flex-wrap: wrap;
            margin-top: 20px;
        }
        
        button {
            background: linear-gradient(to right, #3498db, #2980b9);
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 50px;
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        button:hover {
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(52, 152, 219, 0.4);
        }
        
        button:active {
            transform: translateY(0);
        }
        
        button.mute {
            background: linear-gradient(to right, #e74c3c, #c0392b);
        }
        
        .status {
            text-align: center;
            font-size: 1.1rem;
            font-weight: bold;
            margin: 20px 0;
            color: #2c3e50;
        }
        
        .volume-control {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 15px;
            margin-top: 20px;
        }
        
        input[type="range"] {
            width: 200px;
            height: 8px;
            border-radius: 4px;
            background: #ecf0f1;
            outline: none;
        }
        
        .tips {
            background-color: #e8f4fc;
            border-left: 4px solid #3498db;
            padding: 15px;
            margin: 20px 0;
            border-radius: 0 8px 8px 0;
        }
        
        footer {
            text-align: center;
            padding: 20px;
            background-color: #2c3e50;
            color: white;
        }
        
        @media (max-width: 600px) {
            h1 {
                font-size: 2rem;
            }
            
            .controls {
                flex-direction: column;
            }
            
            button {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>游戏背景音乐实现方案</h1>
            <div class="subtitle">使用HTML5 Audio API添加和控制游戏背景音乐</div>
        </header>
        
        <div class="content">
            <div class="section">
                <h2>基础音频设置</h2>
                <div class="code-block">
                    &lt;audio id="bgm" loop&gt;<br>
                    &nbsp;&nbsp;&lt;source src="bgm.mp3" type="audio/mpeg"&gt;<br>
                    &nbsp;&nbsp;&lt;source src="bgm.ogg" type="audio/ogg"&gt;<br>
                    &lt;/audio&gt;
                </div>
                
                <div class="tips">
                    <p><strong>重要提示：</strong> 提供MP3和OGG两种格式可确保在所有浏览器上的兼容性</p>
                </div>
            </div>
            
            <div class="section">
                <h2>初始化与播放控制</h2>
                <div class="code-block">
                    // 获取音频元素<br>
                    const bgm = document.getElementById('bgm');<br><br>
                    
                    // 设置初始音量 (0.0 - 1.0)<br>
                    bgm.volume = 0.3;<br><br>
                    
                    // 在用户交互后播放<br>
                    function startBGM() {<br>
                    &nbsp;&nbsp;// 尝试播放<br>
                    &nbsp;&nbsp;bgm.play().catch(error => {<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;console.log("自动播放被阻止:", error);<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;// 显示播放按钮<br>
                    &nbsp;&nbsp;});<br>
                    }
                </div>
            </div>
            
            <div class="section">
                <h2>音乐控制面板</h2>
                <div class="status" id="status">音乐状态: 已停止</div>
                
                <div class="controls">
                    <button id="playBtn" onclick="playBGM()">
                        <i class="icon">▶</i> 播放音乐
                    </button>
                    <button id="pauseBtn" onclick="pauseBGM()">
                        <i class="icon">⏸</i> 暂停音乐
                    </button>
                    <button id="muteBtn" class="mute" onclick="toggleMute()">
                        <i class="icon">🔊</i> 静音
                    </button>
                </div>
                
                <div class="volume-control">
                    <span>🔈</span>
                    <input type="range" id="volumeSlider" min="0" max="1" step="0.1" value="0.3" onchange="changeVolume(this.value)">
                    <span>🔊</span>
                </div>
            </div>
            
            <div class="section">
                <h2>解决自动播放限制</h2>
                <div class="code-block">
                    // 首次用户交互时初始化音频<br>
                    document.addEventListener('click', initAudio, { once: true });<br><br>
                    
                    function initAudio() {<br>
                    &nbsp;&nbsp;// 创建音频上下文<br>
                    &nbsp;&nbsp;const audioContext = new (window.AudioContext || window.webkitAudioContext)();<br>
                    &nbsp;&nbsp;const source = audioContext.createBufferSource();<br><br>
                    
                    &nbsp;&nbsp;// 加载音频文件<br>
                    &nbsp;&nbsp;fetch('bgm.mp3')<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;.then(response => response.arrayBuffer())<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;.then(buffer => audioContext.decodeAudioData(buffer))<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;.then(decodedData => {<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;source.buffer = decodedData;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;source.connect(audioContext.destination);<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;source.loop = true;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;source.start(0);<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;});<br>
                    }
                </div>
            </div>
        </div>
        
        <footer>
            <p>HTML5 Audio背景音乐实现方案</p>
        </footer>
    </div>

    <audio id="bgm" loop>
        <!-- 实际项目中替换为你的音乐文件 -->
        <!--可以为网络音频源-->
        <source src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" type="audio/mpeg">
    </audio>

    <script>
        // 获取DOM元素
        const bgm = document.getElementById('bgm');
        const statusDisplay = document.getElementById('status');
        const playBtn = document.getElementById('playBtn');
        const pauseBtn = document.getElementById('pauseBtn');
        const muteBtn = document.getElementById('muteBtn');
        const volumeSlider = document.getElementById('volumeSlider');
        
        // 初始化音量
        bgm.volume = 0.3;
        
        // 播放音乐
        function playBGM() {
            bgm.play()
                .then(() => {
                    statusDisplay.textContent = "音乐状态: 播放中";
                    playBtn.disabled = true;
                    pauseBtn.disabled = false;
                })
                .catch(error => {
                    statusDisplay.textContent = `播放失败: ${error.message}`;
                    console.error("播放失败:", error);
                });
        }
        
        // 暂停音乐
        function pauseBGM() {
            bgm.pause();
            statusDisplay.textContent = "音乐状态: 已暂停";
            playBtn.disabled = false;
            pauseBtn.disabled = true;
        }
        
        // 切换静音
        function toggleMute() {
            bgm.muted = !bgm.muted;
            muteBtn.innerHTML = bgm.muted ? 
                '<i class="icon">✘</i> 取消静音' : 
                '<i class="icon">✔</i> 静音';
            muteBtn.className = bgm.muted ? 'mute' : '';
        }
        
        // 调整音量
        function changeVolume(value) {
            bgm.volume = value;
        }
        
        // 页面加载后尝试自动播放（可能被浏览器阻止）
        window.addEventListener('load', () => {
            // 尝试自动播放
            bgm.play()
                .then(() => {
                    statusDisplay.textContent = "音乐状态: 自动播放中";
                    playBtn.disabled = true;
                    pauseBtn.disabled = false;
                })
                .catch(() => {
                    statusDisplay.textContent = "音乐状态: 点击播放按钮开始";
                });
        });
    </script>
</body>
</html>